package com.lrs.java.j2se_self.net;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class TestTwoclient {
	public static void main(String[] args){
		
		new TFrame().launchFrame();
		
	}
}

class TFrame extends Frame {
	
	TextField tfTxt = null;
	Socket s = null; 
	DataOutputStream dos = null;
	
	public void launchFrame(){
		this.setBounds(400,300,200,400);
		tfTxt = new TextField();
		this.add(tfTxt,BorderLayout.CENTER);
		pack();
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				disconnect();
				System.exit(0);
			}
		});
		tfTxt.addActionListener(new TFListener());
		setVisible(true);
		connect();
	}
	
	public void connect(){
		try {
			s = new Socket("127.0.0.1",5555);
System.out.println("connect");			
			dos = new DataOutputStream(s.getOutputStream());
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void disconnect(){
		
		try {
			dos.close();
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private class TFListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			try {
System.out.println(tfTxt);
				dos.writeUTF(tfTxt.getText());
				tfTxt.setText("");
				dos.flush();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
	}
}